package interfaces.menu;

import java.util.HashMap;

import map.infos.MapDescription;
import map.infos.MapInfos;

import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;


import fileHandling.MapLoader;
import fileHandling.language.Lang;
import fileHandling.language.interfaces.NewGameText;
import interfaces.GUISource;
import interfaces.ImageUpdater;
import interfaces.WordWrapHelper;
import interfaces.language.LanguageButton;
import interfaces.language.LanguageWindow;
import interfaces.superWidgets.InterfaceLabel;

public class DetailsWindow extends LanguageWindow {
	
	private ImageUpdater previewImage;
	private LanguageButton switchButton;
	private String currentMap;
	private InterfaceLabel teamsLabel, descrLabel;
	private HashMap<String, MapDescription> mapDescriptions;
	
	public DetailsWindow(AbsMenu menu, HashMap<String, MapDescription> mapDescriptions) {
		super(true, NewGameText.Map_Details);
		this.mapDescriptions = mapDescriptions;
		
		setSize(menu.getWidth() / 2, menu.getHeight() / 5 * 4);
		setXY(menu.getWidth() / 4, menu.getHeight() / 10);
		
		content.setLayoutManager(new StaticLayout());
		content.getAppearance().add(new PlainBackground(Color.LIGHT_GRAY));
		
		layout();
		
		previewImage = new ImageUpdater(0, 0, 0, 0);
		content.addWidget(previewImage);
		
		changeImageSize(NewGameText.Preview);
		
		switchButton = new LanguageButton(NewGameText.Mini_Map, GUISource.bigFont);
		switchButton.setSize(previewImage.getWidth(), previewImage.getHeight() / 6);
		switchButton.setXY(previewImage.getX(), previewImage.getY() - switchButton.getHeight());
		switchButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				Lang nextLang = NewGameText.Mini_Map;
				String imagePath = "/preview.png";
				
				if(switchButton.getLangText() == NewGameText.Mini_Map) {
					nextLang = NewGameText.Preview;
					imagePath = "/map.png";
				}
				
				String path = MapLoader.MAPS_PATH + currentMap + imagePath;
				previewImage.setImage(path);
				
				changeImageSize(switchButton.getLangText());
				
				switchButton.setLangText(nextLang);
			}
		});
		content.addWidget(switchButton);
		
		int labelWidth = switchButton.getWidth();
		int labelHeight = switchButton.getHeight() / 2;
		
		teamsLabel = new InterfaceLabel(GUISource.bigFont);
		teamsLabel.setSize(labelWidth, labelHeight);
		teamsLabel.setXY(switchButton.getX(), switchButton.getY() - labelHeight * 2);
		content.addWidget(teamsLabel);
		
		descrLabel = new InterfaceLabel(GUISource.bigFont);
		descrLabel.setSize(labelWidth, labelHeight * 4);
		descrLabel.setXY(switchButton.getX(), labelHeight);
		content.addWidget(descrLabel);
		
		layout();
	}
	
	private void changeImageSize(Lang lang) {
		int imageHeight = content.getHeight() / 2;
		int imageWidth = imageHeight / 3 * 4;		
		int imageY = imageHeight;
		
		if(lang == NewGameText.Mini_Map) imageWidth = imageHeight;
			
		int imageX = (content.getWidth() - imageWidth) / 2;
		
		previewImage.setXY(imageX, imageY);
		previewImage.setSize(imageWidth, imageHeight);
	}
	
	public void init(String map) {
		currentMap = map;
		
		String prevPath = MapLoader.MAPS_PATH + currentMap + "/preview.png";
		changeImageSize(NewGameText.Preview);
		previewImage.setImage(prevPath);
		
		switchButton.setLangText(NewGameText.Mini_Map);
		
		MapInfos infos = MapLoader.getMapInfos(currentMap);
		if(infos != null) {
			teamsLabel.setText(infos.getFractionA() + " vs. " + infos.getFractionB());
			setDescription(mapDescriptions.get(currentMap).getDescriptionString());
		}
		
		layout();
	}
	
	@Override
	public void changeLanguage() {
		super.changeLanguage();
		
		if(descrLabel != null && currentMap != null) {
			setDescription(mapDescriptions.get(currentMap).getDescriptionString());
		}
	}
	
	private void setDescription(String descr) {
		WordWrapHelper.wrapWords(descrLabel, descr);
	}
}